<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" v-show="showSearch" :inline="true">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入角色名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="权限字符" prop="key">
        <el-input v-model="queryParams.key" placeholder="请输入权限字符" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Back" @click="handleBack">返回</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="openSelectUser" v-hasPermi="['sys:role:add']">添加角色
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="CircleClose" :disabled="multiple" @click="handleAuthUserAdd"
          v-hasPermi="['sys:role:delete']">取消授权</el-button>
      </el-col>
      <y-right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="roleList" ref="roleRef" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="角色名称" prop="name" :show-overflow-tooltip="true" />
      <el-table-column label="权限字符" prop="key" :show-overflow-tooltip="true" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <y-dict-tag :options="sys_enable_disable" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="create_time" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.create_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center">
        <template #default="scope">
          <el-button link type="primary" icon="CircleClose" @click="handleAuthUserAdd(scope.row)"
            v-hasPermi="['sys:role:delete']">取消授权</el-button>
        </template>
      </el-table-column>
    </el-table>

    <y-pagination v-show="total > 0" :total="total" v-model:page="queryParams.page" v-model:limit="queryParams.limit"
      @pagination="getList" />
    <select-role ref="selectRef" :userId="queryParams.userId" @ok="handleQuery" />
  </div>
</template>

<script setup name="AuthRole">
import selectRole from "./selectRole";
import { relatedRoleList, authRoleRemove } from "@/api/sys/user";

const route = useRoute();
const { proxy } = getCurrentInstance();
const { sys_enable_disable } = proxy.useDict("sys_enable_disable");

const roleList = ref([]);
const loading = ref(true);
const showSearch = ref(false);
const multiple = ref(true);
const total = ref(0);
const roleIds = ref([]);
const roleNames = ref([]);

const queryParams = reactive({
  userId: route.params.userId,
  name: undefined,
  key: undefined,
  page: 1,
  limit: 10,
});

/** 查询授权用户列表 */
function getList() {
  loading.value = true;
  relatedRoleList(queryParams).then((response) => {
    roleList.value = response.data;
    total.value = response.total;
    loading.value = false;
  });
}

/** 返回按钮操作 */
function handleBack() {
  proxy.$router.back();
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.page = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  roleIds.value = selection.map((item) => item.id);
  roleNames.value = selection.map((item) => item.name);
  multiple.value = !selection.length;
}
/** 打开授权用户表弹窗 */
function openSelectUser() {
  proxy.$refs["selectRef"].show();
}
/** 取消授权按钮操作 */
function handleAuthUserAdd(row) {
  roleIds.value = [row.id] || roleIds.value;
  roleNames.value = row.name || roleNames.value;
  proxy.$modal
    .confirm('确认要取消角色"' + roleNames.value + '"的授权吗？')
    .then(function () {
      return authRoleRemove({
        userId: queryParams.userId,
        roleIds: roleIds.value,
      });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("取消授权成功");
    })
    .catch(() => { });
}

onMounted(() => {
  getList();
})
</script>
